import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

// 静态路由
const constantRouterMap = [{
  path: '/login',
  name: 'login',
  component: () => import('@/views/Login')
}, {
  path: '/main',
  name: 'main',
  component: () => import('@/views/Main'),
  redirect: '/main/personal',
  children: [{
    path: 'personal',
    name: 'Personal',
    component: () => import('@/views/Personal')
  }]
}]

// 动态路由
const asyncRouterMap = [{
  path: '/sys',
  name: 'sys',
  component: () => import('@/views/Main'),
  meta: {
    menu: true,
    title: '系统管理',
    icon: 'iconfont icon-xitong'
  },
  children: [{
    path: 'index',
    name: 'Index',
    component: () => import('@/views/Index'),
    meta: {
      menu: true,
      title: '首页',
      icon: 'iconfont icon-home'
    }
  }, {
    path: 'job',
    name: 'Job',
    component: () => import('@/views/sys/Job.vue'),
    meta: {
      menu: true,
      title: '任务管理',
      icon: 'iconfont icon-renwu'
    }
  }, {
    path: 'activity',
    name: 'SysActivity',
    component: () => import('@/views/sys/Activity.vue'),
    meta: {
      menu: true,
      title: '活动管理',
      icon: 'iconfont icon-tubiaolunkuohua03'
    }
  }]
}, {
  path: '/xcx',
  name: 'xcx',
  component: () => import('@/views/Main'),
  redirect: '/xcx/user',
  meta: {
    menu: true,
    title: '小程序管理',
    icon: 'iconfont icon-xiaochengxu'
  },
  children: [{
    path: 'user',
    name: 'XcxUser',
    component: () => import('@/views/xcx/User.vue'),
    meta: {
      menu: true,
      title: '用户管理',
      icon: 'iconfont icon-yonghu'
    }
  }, {
    path: 'order',
    name: 'Order',
    component: () => import('@/views/xcx/Order.vue'),
    meta: {
      menu: true,
      title: '订单管理',
      icon: 'iconfont icon-dingdan'
    }
  }, {
    path: 'withdraw',
    name: 'Withdraw',
    component: () => import('@/views/xcx/Withdraw.vue'),
    meta: {
      menu: true,
      title: '提现管理',
      icon: 'iconfont icon-tixian'
    }
  }, {
    path: 'activity',
    name: 'XcxActivity',
    component: () => import('@/views/xcx/Activity.vue'),
    meta: {
      menu: true,
      icon: 'iconfont icon-tubiaolunkuohua03',
      title: '活动管理'
    }
  }, {
    path: 'commonText',
    name: 'CommonText',
    component: () => import('@/views/xcx/CommonText.vue'),
    meta: {
      menu: true,
      title: '文本管理',
      icon: 'iconfont icon-wenben'
    }
  }, {
    path: 'imageInfo',
    name: 'ImageInfo',
    component: () => import('@/views/xcx/ImageInfo.vue'),
    meta: {
      menu: true,
      title: '图片管理',
      icon: 'iconfont icon-tupian'
    }
  }]
}]

const createRouter = () => new VueRouter({
  routes: constantRouterMap
})

const router = createRouter()

export function initRoute() {
  router.options.routes = constantRouterMap.concat(asyncRouterMap)
  router.addRoutes(asyncRouterMap)
}

// 路由请求之前查看是否已经登录
router.beforeEach((to, from, next) => {
  NProgress.start()
  const token = sessionStorage.getItem('token')
  if (!token) {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  } else {
    if (to.path === '/login') {
      next('/main')
    } else {
      next()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

// 解决重复点击跳转路由的时候报的不影响程序运行的错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
